//
//  PersonalProfileSiginViewController.m
//  ChatForHot
//
//  Created by wr on 2018/10/15.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PersonalProfileSiginViewController.h"

@interface PersonalProfileSiginViewController ()<YYTextViewDelegate>

@property (nonatomic, strong) UIButton *HOTCHATsaveButton;
@property (nonatomic, strong) UIView *HOTCHATtextBackview;
@property (nonatomic, strong) YYTextView *HOTCHATintroTextView;
@property (nonatomic, strong) UILabel *HOTCHATsurplusNumLab;
@property (nonatomic, strong, readwrite) RACSubject *shouldReloadDataSubejct;
@end

@implementation PersonalProfileSiginViewController

- (void)HOTCHATdidInitialized {
    [super HOTCHATdidInitialized];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.shouldReloadDataSubejct = [RACSubject subject];
}

- (void)HOTCHATInsertSubViews {
    [super HOTCHATInsertSubViews];
    
    self.HOTCHATtextBackview = [[UIView alloc] init];
    self.HOTCHATtextBackview.layer.masksToBounds = YES;
    self.HOTCHATtextBackview.layer.borderColor = [UIColor colorFromHexString:@"#B9B9B9"].CGColor;
    self.HOTCHATtextBackview.layer.borderWidth = PixelOne;
    self.HOTCHATtextBackview.layer.cornerRadius = 5;
    self.HOTCHATtextBackview.backgroundColor = [UIColor colorFromHexString:@"#F4F4F4"];
    [self.view addSubview:self.HOTCHATtextBackview];
    
    self.HOTCHATsaveButton = [[UIButton alloc]init];
    [self.HOTCHATsaveButton setBackgroundImage:[[UIImage imageNamed:@"personal_iden_sav_n"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 15, 15, 15) resizingMode:(UIImageResizingModeStretch)] forState:UIControlStateNormal];
    [self.HOTCHATsaveButton setBackgroundImage:[[UIImage imageNamed:@"personal_iden_sav_h"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 15, 15, 15) resizingMode:(UIImageResizingModeStretch)] forState:UIControlStateHighlighted];
    [self.HOTCHATsaveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.HOTCHATsaveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [self.HOTCHATsaveButton setTitle:NSLocalizedString(@"SAVEBUTTON", nil) forState:UIControlStateNormal];
    [self.HOTCHATsaveButton addTarget:self action:@selector(introTextSaveAction) forControlEvents:UIControlEventTouchUpInside];
    self.HOTCHATsaveButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:self.HOTCHATsaveButton];
    
    self.HOTCHATintroTextView = [[YYTextView alloc] init];
    self.HOTCHATintroTextView.delegate = self;
    self.HOTCHATintroTextView.textVerticalAlignment = YYTextVerticalAlignmentTop;
    self.HOTCHATintroTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 7.0, 0);
    self.HOTCHATintroTextView.scrollEnabled = YES;
    self.HOTCHATintroTextView.font = [UIFont systemFontOfSize:15];
    self.HOTCHATintroTextView.textColor = [UIColor colorWithHexString:@"AAAAAA"];
    self.HOTCHATintroTextView.placeholderTextColor = [UIColor colorFromHexString:@"#AAAAAA"];
    self.HOTCHATintroTextView.placeholderText = NSLocalizedString(@"profileHe4", nil);
    [self.HOTCHATtextBackview addSubview:self.HOTCHATintroTextView];
    
    self.HOTCHATsurplusNumLab = [[UILabel alloc] init];
    self.HOTCHATsurplusNumLab.font = [UIFont systemFontOfSize:13];
    self.HOTCHATsurplusNumLab.textColor = [UIColor colorFromHexString:@"#AAAAAA"];
    self.HOTCHATsurplusNumLab.textAlignment = NSTextAlignmentRight;
    self.HOTCHATsurplusNumLab.text = @"100";
    [self.HOTCHATtextBackview addSubview:self.HOTCHATsurplusNumLab];
}

- (void)HOTCHATsetNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated {
    [super HOTCHATsetNavigationItemsIsInEditMode:isInEditMode animated:animated];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backBtnImg"] style:UIBarButtonItemStylePlain target:self action:@selector(HOTCHATbackClcik)];
    self.title = NSLocalizedString(@"profile11", nil);
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.HOTCHATintroTextView.text = self.model.signature;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.HOTCHATtextBackview.size = CGSizeMake(kScreenWidth - 30, 150);
    self.HOTCHATtextBackview.top = 15;
    self.HOTCHATtextBackview.left = 15;
    
    self.HOTCHATintroTextView.size = CGSizeMake(kScreenWidth - 30 - 30, 90);
    self.HOTCHATintroTextView.top = 10;
    self.HOTCHATintroTextView.left = 15;
    
    self.HOTCHATsurplusNumLab.size = CGSizeMake(kScreenWidth - 60, 20);
    self.HOTCHATsurplusNumLab.bottom = self.HOTCHATtextBackview.bottom - 18;
    self.HOTCHATsurplusNumLab.left = 22;
    
    self.HOTCHATsaveButton.size = CGSizeMake(kScreenWidth - 30, 44);
    self.HOTCHATsaveButton.top = self.HOTCHATtextBackview.bottom + 15;
    self.HOTCHATsaveButton.left = 15;
}

- (void)textViewDidChange:(YYTextView *)textView {
    self.HOTCHATsurplusNumLab.text = [NSString stringWithFormat:@"%zd",[textView.text HOTCHATleftCharWithmaxLength:100]];
}

- (void)introTextSaveAction {
    @weakify(self)
    NSString *signature = [self.HOTCHATintroTextView.text HOTCHATmaxLength:100];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    APPINFO *appInfo = [APPINFO defaultManager];
    [dic setValue:appInfo.accountid forKey:@"accountid"];
    [dic setValue:signature  forKey:@"signature"];
    
    [self HOTCHATstartLoading];
    [NetWorkTool HOTCHATRequestWithStringURL:UPDATEAccountINFO_URL progress:^(NSProgress * _Nonnull progress) {
    } RequestWay:@"POST" Parma:dic SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
        @strongify(self)
        if (isSucess) {
            self.model.signature = signature;
            [NetWorkTool HOTCHATUpdateToken];
            [self HOTCHATstopLoading];
            [self.shouldReloadDataSubejct sendNext:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } FailureBlock:^(NSError * _Nonnull error) {
    }];
}
@end
